﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;

namespace ShopShop.Models
{
    public partial class ShoppingCart
    {
        BanGiayEntities db = new BanGiayEntities();
        public string shoppingCartId { get; set; }
        public const string CartSessionKey = "CartID";

        public static ShoppingCart GetCart(HttpContextBase context)
        {
            var cart = new ShoppingCart();
            cart.shoppingCartId = cart.GetCartId(context);
            return cart;
        }

        // Helper method to simplify shopping cart calls
        public static ShoppingCart GetCart(Controller controller)
        {
            return GetCart(controller.HttpContext);
        }

        public string GetCartId(HttpContextBase context)
        {
            if (context.Session[CartSessionKey] == null)
            {
                if (!string.IsNullOrWhiteSpace(context.User.Identity.Name))
                {
                    context.Session[CartSessionKey] =
                        context.User.Identity.Name;
                }
                else
                {
                    // Generate a new random GUID using System.Guid class
                    Guid tempCartId = Guid.NewGuid();
                    // Send tempCartId back to client as a cookie
                    context.Session[CartSessionKey] = tempCartId.ToString();
                }
            }
            return context.Session[CartSessionKey].ToString();
        }


        public void AddToCart(Product pro)
        {
            Cart cartItem = db.Carts.SingleOrDefault(x => x.CartId == shoppingCartId && x.ProductId == pro.ProductId);
            //if new item , add to cart
            if (cartItem == null)
            {
                cartItem = new Cart
                {
                    CartId = shoppingCartId,
                    ProductId = pro.ProductId,
                    Count = 1,
                    AccountId = null,
                    DateCreated = System.DateTime.Now
                };
                db.Carts.Add(cartItem);
            }
            else
            {
                cartItem.Count++;                
            }
            db.SaveChanges();
        }

        public void RemoveFromCart(int cartID)
        {
            Cart cartItem = db.Carts.SingleOrDefault(x => x.RecordId == cartID && x.CartId == shoppingCartId);
            if (cartItem != null)
                db.Carts.Remove(cartItem);
        }

        public void EmptyCart()
        {
            var cartItem = db.Carts.Where(x => x.CartId == shoppingCartId);
            foreach (Cart cart in cartItem)
                db.Carts.Remove(cart);
            db.SaveChanges();
        }

        public List<Cart> GetCart()
        {
            return db.Carts.Where(x => x.CartId == shoppingCartId).ToList();
        }

        public int sumCart()
        {
            int? sum = (from c in db.Carts
                        where c.CartId == shoppingCartId
                        select c.Count).Sum();
            return sum ?? 0;
        }

        public decimal getTotal()
        {
            decimal? total = (from c in db.Carts
                              where c.CartId == shoppingCartId
                              select c.Count * c.Product.Price).Sum();

            return total ?? decimal.Zero;
        }



    }
}